package com.tz.farmer.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.smartPark.msframe.result.ResultEx;
import com.tz.farmer.model.FarmerProcess;
import com.tz.farmer.service.IFarmerProcessService;
import com.tz.farmer.service.IFarmerSunDataService;
import com.tz.farmer.so.DefaultSo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.Map;
import java.util.TreeMap;

@Slf4j
@RestController
@RequestMapping("/fishing")
@Api(value = "阳光渔场", tags = "阳光渔场-养殖接口")
public class FishingController {

    @Autowired
    private IFarmerSunDataService dataService;

    @Autowired
    private IFarmerProcessService processService;

    private static final String boatKey = "fishingBoat";
    private static final String typeKey = "fishingType";
    private static final String areaKey = "fishingArea";
    private static final String market = "阳光渔场-捕捞";
    private static final String channel = "抽检";

    @GetMapping("/get")
    public ResultEx summary() {
        Map<String, Object> response = new TreeMap<>();

        response.put("boat", dataService.getData(market, boatKey));
        response.put("type", dataService.getData(market, typeKey));
        response.put("area", dataService.getData(market, areaKey));

        return ResultEx.builder().makeSuccessResult(response);
    }

    @PostMapping("/set-boat")
    @ApiOperation(value = "设置渔船数量")
    @ApiImplicitParam(name = "count", value = "数量", dataType = "Integer")
    public ResultEx setBoat(BigDecimal count) {
        if (ObjectUtils.isEmpty(count) || count.doubleValue() <= 0) {
            return ResultEx.builder().makeFailedResult("参数[count]值错误");
        }

        if (dataService.setData(market, boatKey, count.doubleValue())) {
            return ResultEx.builder().makeSuccessResult();
        }

        return ResultEx.builder().makeFailedResult();
    }

    @PostMapping("/set-type")
    @ApiOperation(value = "设置渔获种类")
    @ApiImplicitParam(name = "count", value = "数量", dataType = "Integer")
    public ResultEx setType(BigDecimal count) {
        if (ObjectUtils.isEmpty(count) || count.doubleValue() <= 0) {
            return ResultEx.builder().makeFailedResult("参数[count]值错误");
        }

        if (dataService.setData(market, typeKey, count.doubleValue())) {
            return ResultEx.builder().makeSuccessResult();
        }

        return ResultEx.builder().makeFailedResult();
    }

    @PostMapping("/set-area")
    @ApiOperation(value = "设置作业渔区数量")
    @ApiImplicitParam(name = "count", value = "数量", dataType = "Integer")
    public ResultEx setArea(BigDecimal count) {
        if (ObjectUtils.isEmpty(count) || count.doubleValue() <= 0) {
            return ResultEx.builder().makeFailedResult("参数[count]值错误");
        }

        if (dataService.setData(market, areaKey, count.doubleValue())) {
            return ResultEx.builder().makeSuccessResult();
        }

        return ResultEx.builder().makeFailedResult();
    }

    @PostMapping("/list")
    @ApiOperation(value = "渔获物信息列表")
    public ResultEx list(@RequestBody @Validated DefaultSo body) {
        IPage<FarmerProcess> res = processService.selectPage(body, market);

        return ResultEx.builder().makeSuccessResult(res);
    }

    @PostMapping("/add-process")
    @ApiOperation(value = "添加渔获物数据")
    public ResultEx addProcess(@RequestBody @Validated FarmerProcess body) {
        return processService.setProcess(body, market);
    }
}
